package ru.pashkan.perspectives.elements;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ru.pashkan.entity.Site;
import ru.pashkan.db.dao.SitesDao;

public class SitesListTable {
    private Table sitesTable;

    public SitesListTable(Display display) {

        Shell shell = display.getActiveShell();
        sitesTable = new Table(shell,  SWT.CHECK | SWT.BORDER | SWT.FULL_SELECTION | SWT.V_SCROLL);
        sitesTable.setLinesVisible(true);
        sitesTable.setHeaderVisible(true);

        final GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
        sitesTable.setLayoutData(gridData);

        getSitesListHead();
        getSitesListItems();
    }

    private void getSitesListItems(){
        ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext-db.xml"});
        SitesDao siteDao = (SitesDao) appContext.getBean("SitesDao");

        int i = 0;
        while(i < siteDao.takeSite().size()){

            System.out.println(siteDao.takeSite().get(i).getSiteName() + " - " + siteDao.takeSite().get(i).getSiteUrl());

//        }



                TableItem item = new TableItem(sitesTable, SWT.CENTER);

            if(siteDao.takeSite().get(i).getSiteName() != null)
                item.setText(1, siteDao.takeSite().get(i).getSiteName());
            if(siteDao.takeSite().get(i).getSiteUrl() != null)
                item.setText(2, siteDao.takeSite().get(i).getSiteUrl());

            if(String.valueOf(siteDao.takeSite().get(i).getClientID()) != null)
                item.setText(4, String.valueOf(siteDao.takeSite().get(i).getClientID()));
				/*int idType = 0;
				idType = resultSet.getInt("type");
				String siteType = this.dbTable.getSitesTypeSet(idType);
				item.setText(3, siteType);		*/



//
                //item.setText(5, resultSet.getString("startDate"));
                //item.setText(6, resultSet.getString("endDate"));

            i++;
        }
        //Высота строчек
        sitesTable.addListener(SWT.MeasureItem, new Listener(){
            public void handleEvent(Event event) {
                event.height = 20;
            }
        });
		/*-------------------------------*/
    }

    private void getSitesListHead(){
        TableColumn column;
        column = new TableColumn(sitesTable, SWT.CENTER);
        column.setWidth(30);

        column = new TableColumn(sitesTable, SWT.CENTER);
        //column.setResizable(false);
        column.setText("Имя сайта");
        column.setToolTipText("Имя сайта");
        column.setWidth(120);

        column = new TableColumn(sitesTable, SWT.CENTER);
        column.setText("URL");
        column.setWidth(120);

        column = new TableColumn(sitesTable, SWT.CENTER);
        column.setText("Тип сайта");
        column.setWidth(120);

        column = new TableColumn(sitesTable, SWT.CENTER);
        column.setText("Клиент");
        column.setWidth(120);

        column = new TableColumn(sitesTable, SWT.CENTER);
        column.setText("Дата начала");
        column.setWidth(120);

        column = new TableColumn(sitesTable, SWT.CENTER);
        column.setText("Дата сдачи");
        column.setWidth(120);


    }


}
